package com.example.application.compose;

import java.util.HashMap;
import java.util.Set;

import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode;

import org.apache.commons.lang3.StringUtils;
import org.vaadin.olli.ClipboardHelper;

// @JavaScript("./src/main/resources/META-INF/resources/frontend/clipboard.min.js")
// @JavaScript(value="./clipboard.min.js",loadMode=LoadMode.LAZY)
// @JsModule(value="./clipboard.min.js")
public class Button_Deal extends Composite<Div> {
    private TextField textField;
    private TextField resField;
    private Button b2;
    // private NativeButton button;
    private String res_str="";
    public static HashMap<String,String> pair=new HashMap<>(){{
        put("[","{");
        put("]","}");
        put("\"","\'");
        put(" ", "");
    }};

    
    public String getRes_str() {
        return res_str;
    }


    public void setRes_str(String res_str) {
        this.res_str = res_str;
    }


    public Button_Deal(String labelText, String value) {
        b2=new Button(value);
        textField = new TextField();
        resField=new TextField();

        resField.setMinWidth("40em");

        resField.setReadOnly(true);
        // resField.setValue(res_str);
        textField.setValueChangeMode(ValueChangeMode.EAGER);
        // textField.setValueChangeTimeout(5);

        // resField.setValue(res_str);
        resField.setId("my_copy");



        textField.setLabel(labelText);
        textField.setHelperText("帮助文本");
        textField.setMinWidth("40em");
        // textField.setValue("Ruukinkatu 2");
        textField.setPlaceholder("输入");
        textField.setClearButtonVisible(true);
        textField.setPrefixComponent(VaadinIcon.MAP_MARKER.create());
        textField.addInputListener(event->{
            String src_str=textField.getValue();
            // resField.setValue(textField.getValue());
            // resField.setValue(Deal(src_str));
            resField.setValue(Deal(src_str));
        });
        // button =new NativeButton(value);
        // button.setText(value);
        // button.setClassName("btn");
        // button.getElement().setAttribute("data-clipboard-target", "#my_copy");


        ClipboardHelper clipboardHelper = new ClipboardHelper("Copy", b2);
        // clipboardHelper.setContent(resField.getValue());
        b2.addFocusListener(event->{
            System.out.println(resField.getValue());
            clipboardHelper.setContent(resField.getValue());
        });
        // button.getElement().setAttribute("class", "btn");

        // Binder<String> bind=new Binder<>(String.class);
        // bind.bindReadOnly(resField, res_str);
        // bind.forField(resField).bindReadOnly(res_str);
        // resField.addValueChangeListener(listener);
        // resField.setEnabled(false);
        Div one=new Div();
        one.add(resField);
        one.add(clipboardHelper);
        // one.add(button);


        // add(clipboardHelper);

        // getContent().setWidthFull();
        getContent().add(textField, one);

        // Page page=UI.getCurrent().getPage();
        // page.executeJs("new ClipboardJS('.btn');",value);
        // getElement().executeJs("new ClipboardJS('.btn');",value);
        // getElement().executeJs("console.log('hi')",value);

    }


    private String Deal(String src_str) {
        Set<String> keySet = pair.keySet();
        // StringBuffer sBuffer=new StringBuffer();
        String res=src_str;
        
        for (String string : keySet) {
            res=StringUtils.replace(res, string, pair.get(string));
        }
        // StringUtils.replace(text, searchString, replacement);
        return res;
    }
}
